package generator.biz;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import generator.StuVo.StuVo;
import generator.mapper.Addr;
import generator.mapper.Stu;
import generator.service.AddrService;
import generator.service.StuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class StuBizImpl implements StuBiz {

    @Autowired
    private StuService stuService;

    @Autowired
    private AddrService addrService;

    @Override
    public void addUser(Stu stu) {
        stuService.save(stu);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addStu(Stu stu) {
        stuService.save(stu);
    }

    @Override
    public List<Stu> findAll() {
        return stuService.list();
    }

    @Override
    public void updateUserByName(Stu stu) {
        if (stu.getId() == null) {
            throw new RuntimeException("id不能为空");
        }
        Stu byId = stuService.getById(stu.getId());
        if (byId != null) {
            byId.setName(stu.getName());
            byId.setSex(stu.getSex());
            stuService.updateById(byId);
        }
    }

    @Override
    public void deleteById(Integer id) {
        stuService.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public StuVo getStuVoById(Integer id) {
        Stu stu = stuService.getById(id);
        if (id == null) {
            throw new RuntimeException("id不能为空");
        }
        StuVo stuVo = new StuVo();
        if (id != null) {
            Addr addr = addrService.getById(stu.getId());
            stuVo.setStuId(stu.getId());
            stuVo.setStuName(stu.getName());
            stuVo.setStuSex(stu.getSex());
            stuVo.setAddress(addr.getAddress());
        }
        Addr one = addrService.getOne(new LambdaQueryWrapper<Addr>().eq(Addr::getStuId, id).last("limit 1"));
        if (one != null) {
            stuVo.setAddress(one.getAddress());
        }
        return stuVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addStuInfo(StuVo stuVo) {
        Stu stu = new Stu();
        String stuName = stuVo.getStuName();
        stu.setName(stuName);
        String stuSex = stuVo.getStuSex();
        stu.setSex(stuSex);
        stuService.save(stu);
        log.info("stuVo:{}新增成功", stu.getId());
        Addr addr = new Addr();
        String address = stuVo.getAddress();
        addr.setAddress(address);
        addrService.save(addr);
        log.info("addrVo:{}新增成功", addr.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteVoById(Integer id) {
        stuService.removeById(id);
        addrService.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStuVoById(StuVo stuVo) {
        if (stuVo.getStuId() == null) {
            throw new RuntimeException("id不能为空");
        }
        if (stuVo.getStuName()!= null) {
            Stu stu = stuService.getById(stuVo.getStuId());
            Addr addr = addrService.getById(stuVo.getStuId());
            stu.setName(stuVo.getStuName());
            stu.setSex(stuVo.getStuSex());
            addr.setAddress(stuVo.getAddress());
            addrService.updateById(addr);
            stuService.updateById(stu);
        }
    }
}
